// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.sqldb;

import java.sql.SQLException;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.designer.core.i18n.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class StartHsqldbServer extends Action {

    private static boolean isRunning = false;

    private IJavaProject currentProject;

    public StartHsqldbServer() {
        super(Messages.getString("DesignerCoreService.Start.Label")); //$NON-NLS-1$
        setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.ACTION_STOPDB));
    }

    @Override
    public void run() {
        currentProject = DenguesCorePlugin.getDefault().getDesignerCoreService().getJavaProcessor().getJavaProject();

        try {
            if (!isRunning) {
                SqldbServerTracker.getDefault().startHsqldbServer(currentProject);
                isRunning = true;
            } else {
                SqldbServerTracker.getDefault().stopHsqldbServer();
                isRunning = false;
            }
            updateUi();
        } catch (CoreException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Sets the isStart.
     * 
     * @param isStart the isStart to set
     */
    public static void setRunning(boolean isRunning) {
        StartHsqldbServer.isRunning = isRunning;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "update".
     * 
     * @param value
     */
    public void update(Boolean value) {
        StartHsqldbServer.isRunning = value;
        updateUi();
    }

    private void updateUi() {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (isRunning) {
            if (window != null) {
                ((ApplicationWindow) window).setStatus(Messages.getString("StartHsqldbServer.Start.Status")); //$NON-NLS-1$
            }
            setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.ACTION_STARTDB));
            setText(Messages.getString("DesignerCoreService.Stop.Label")); //$NON-NLS-1$
        } else {
            if (window != null) {
                ((ApplicationWindow) window).setStatus(Messages.getString("StartHsqldbServer.Stop.Status")); //$NON-NLS-1$
            }
            setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.ACTION_STOPDB));
            setText(Messages.getString("DesignerCoreService.Start.Label")); //$NON-NLS-1$
        }
    }
}
